package tddd24.ajgallery.client;

import java.util.ArrayList;

import tddd24.ajgallery.client.gwtphotoalbum.GWTPhotoAlbum;
import tddd24.ajgallery.shared.Base64;
import tddd24.ajgallery.shared.FieldVerifier;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.Widget;



// running python scripts from java:
// http://www.programmersheaven.com/mb/python/415726/415726/invoking-python-script-from-java/

/*
 * done:
 * Image upload function: 
 * select and upload pictures from computer. 
 * Implement picture sending to server.
 * 
 * TODO:
 * Server side: 
 * Run python scripts for updating album.
 * register pictures in a database, store on server. 
 * 
 * Send pictures to client on request
 * 
 * */



/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Aj_gallery implements EntryPoint {
	
	private final String UB_WIDTH = "200";
//	private final int bufSize = 1073741824 * 5; // 5 MB
	
	private VerticalPanel mainTable;

	private FlexTable contentTable;	
	private FlexTable menuColumn;
	private FlexTable header;
	
	private Viewer galleryView;
	private Button galleryBut;
	private Button uploadBut;
	
	private GWTFileUpload uploader;
	
	private ajGalleryServiceAsync galleryServ = GWT.create(ajGalleryService.class);
	
	
	private static final String IMAGE_URL = GWT.getHostPageBaseURL() + "images";
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";

	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
//	private final GreetingServiceAsync greetingService = GWT.create(GreetingService.class);

	/**
	 * mainTable <- [contentTable] <- [menuColumn, textArea]
	 * menuColumn <- [galleryBut]
	 */
	public void onModuleLoad() {
		
		setUpComponents();
		
		menuColumn.setWidget(0, 0, galleryBut);	
		
		contentTable.setWidget(0, 0, menuColumn);	

		contentTable.setWidget(0, 1, uploadBut);
		mainTable.add(header);
		galleryView = new Viewer(IMAGE_URL, mainTable);
		
		mainTable.add(contentTable);
		RootPanel.get("mainTableContainer").add(mainTable);
		
	}
	
	
	public void setUpComponents(){
		
		mainTable = new VerticalPanel();
		
		contentTable = new FlexTable();
		
		contentTable.getCellFormatter().addStyleName(1, 1, "text");
		contentTable.setStyleName("table");
		contentTable.setCellSpacing(10);
		
		menuColumn = new FlexTable();
		menuColumn.addStyleName("menu");
		
		header = new FlexTable();
		header.addStyleName("header");
		header.setText(0, 0, "AJ-Gallery");

		
		uploadBut = new Button("Upload");
		uploadBut.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {		
				uploader = new GWTFileUpload(2, 1, contentTable);
			}
		});
		
		galleryBut = new Button("View Gallery");
		galleryBut.addStyleName("button");
		
		galleryBut.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				contentTable.setVisible(false);
				header.setVisible(false);
				galleryView.show();
			}
		});	
	}
	
//	public void addImage(String url){
//		displayedImage.setUrl(url);
//		
//		displayedImage.setVisible(true);
//	}
}	
	
//	private void processFileUpload(String pathname){
//		byte[] fileContent = loadFile(pathname);	
//		contentTable.setText(2, 0, pathname + " was read");
//	}
//	
//	private byte[] loadFile(String pathname){
//		try {
//			FileInputStream in = new FileInputStream(pathname);
//		
//		byte[] buff = new byte[bufSize];
//	
//		try {
//			int bytes = in.read(buff);	
//			return buff;	
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		
//		} catch (FileNotFoundException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		return new byte[1];
//	}
	
	
//	
//	private void updateGallery(GWTPhotoAlbum input)
//	{
//
//		// Initialize the service proxy.
//		if (galleryServ == null) {
//			galleryServ = GWT.create(ajGalleryService.class);
//		}
//
//		// Set up the callback object.
//		AsyncCallback<ArrayList<String[]>> callback = new AsyncCallback<ArrayList<String[]>>() {
//			public void onFailure(Throwable caught) {
//				// TODO: Do something with errors.
//			}
//
//			public void onSuccess(ArrayList<String[]> result) {
//				// do something
//			}
//		};
//
//		// Make the call to the stock price service.
//		galleryServ.processInput("a string", callback);
//	}
//	
//}
